﻿Imports System.Reflection
Imports System.Drawing
Imports System.IO
Imports System.Windows.Forms
Imports System.Threading
Imports System.Drawing.Imaging
'Imports Microsoft.VisualBasic.Strings

Public Class Codigo39
    Dim bmp As Bitmap

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
    Public Function Code3of9(ByVal Code As String, ByRef PaintInto As System.Drawing.Bitmap) As String
        Dim sValidChars As String
        Dim sValidCodes As String
        Dim lElevate As Integer
        Dim lCounter As Long
        Dim lWkValue As Long
        Dim PosX As Long
        Dim PosY1 As Long
        Dim PosY2 As Long
        Dim Tamaño_Letra As Integer = PaintInto.Height * 0.1


        If PaintInto.Height < 30 Then
            Return "El tamaño minimo para el alto de el codigo de barras es de 30."
        End If

        Code = UCase(IIf(Microsoft.VisualBasic.Strings.Left(Code, 1) = "*", "", "*") + Code + IIf(Microsoft.VisualBasic.Strings.Right(Code, 1) = "*", "", "*"))

        PosX = ((Len(Code) * 16)) - 1
        PosX = (PaintInto.Width - PosX) \ 2

        If PosX < 0 Then
            Return "El tamaño del codigo no entra en la imagen."
        End If

        'Limpio el Paint
        Dim GR As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(PaintInto)
        GR.SmoothingMode = Drawing2D.SmoothingMode.None
        GR.FillRectangle(Brushes.White, 0, 0, PaintInto.Width, PaintInto.Height)

        sValidChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*"

        sValidCodes = "41914595664727860970419025962647338417105957" + _
        "84729059950476626106644590602984801043246599" + _
        "62476744460260046477586109044686603224803443" + _
        "91860130478424477058030365265828235758580903" + _
        "65863556658042365383495434978353624150635770"

        If Tamaño_Letra > 8 Then
            'Posicion
            PosY1 = PaintInto.Height * 0.85
            'Largo de las barras
            PosY2 = PaintInto.Height * 0.02
        Else
            'Posicion
            PosY1 = PaintInto.Height * 0.6
            'Largo de las barras
            PosY2 = PaintInto.Height * 0.02
        End If


        For lCounter = 1 To Len(Code)
            'Here is where the number is fetched fro
            '     m the sValidCodes string. It will get on
            '     ly 5 digits.
            lWkValue = Val(Mid(sValidCodes, ((InStr(1, sValidChars, Mid(Code, lCounter, 1)) - 1) * 5) + 1, 5))
            lWkValue = IIf(lWkValue = 0, 36538, lWkValue)

            For lElevate = 15 To 0 Step -1
                'It evaluates the binary number to see i
                '     f it has to draw a line.
                If lWkValue >= 2 ^ lElevate Then
                    GR.DrawLine(Pens.Black, PosX, PosY1, PosX, PosY2)
                    'pPaintInto.Line (PosY1, PosX)-(PosY2, PosX)
                    lWkValue = lWkValue - (2 ^ lElevate)
                End If
                PosX = PosX + 1
            Next
        Next

        Dim fnt As Font
        Dim rec As System.Drawing.RectangleF

        If Tamaño_Letra > 8 Then
            fnt = New Font("Courier New", Tamaño_Letra)
            rec = New System.Drawing.RectangleF(1, PaintInto.Height * 0.86, PaintInto.Width - 1, PaintInto.Height * 0.14)
        Else
            Tamaño_Letra = PaintInto.Height * 0.25
            fnt = New Font("Courier New", Tamaño_Letra)
            rec = New System.Drawing.RectangleF(1, PaintInto.Height * 0.65, PaintInto.Width - 1, PaintInto.Height * 0.32)
        End If




        Dim formato As New StringFormat
        formato.Alignment = StringAlignment.Center
        formato.LineAlignment = StringAlignment.Center
        GR.DrawString(Code, fnt, Brushes.Black, rec, formato)

        'Dim tmp As New Bitmap(PaintInto.Width, PaintInto.Height, PaintInto.PixelFormat)
        'Dim pix As Color

        'Dim tol As Integer = 10

        'For v As Integer = 1 To 3
        '    For ix As Integer = 0 To PaintInto.Width - 1
        '        For jy As Integer = 0 To PaintInto.Height - 1
        '            pix = PaintInto.GetPixel(ix, jy)

        '            If (pix.R) >= tol And (pix.G) >= tol And (pix.B) >= tol Then
        '                tmp.SetPixel(ix, jy, Color.White)
        '            Else
        '                tmp.SetPixel(ix, jy, Color.Black)
        '            End If
        '            'tmp.SetPixel(ix, jy, pix)
        '        Next
        '    Next
        '    PaintInto = tmp
        'Next

        'PaintInto = tmp

        Return ""

    End Function

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        bmp.Save("C:\codigo.png", System.Drawing.Imaging.ImageFormat.Png)
    End Sub

    Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        bmp = New Bitmap(PictureBox1.Width, PictureBox1.Height)
        Code3of9("040000594086 6", bmp)
        PictureBox1.Image = bmp
    End Sub
End Class